﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
namespace hconnect {
    public abstract class SiemensConnectBase :SocketZtTaskConnect {
        public int initfinsh = 0;
        ///<summary >”1”: S7-200/smart系列，”2”： S7-300/400/1200/1500系列；</summary>
        protected string PLCtype = "2";
        ///<summary >S7-200/Smart需要用的参数，S7-200："10 11"，Smart："02 01" 。其他PLC忽略</summary>
        private string localTASP = "02 00";
        ///<summary >S7-200/Smart需要用的参数，S7-200："10 01"，Smart："02 00"。其他PLC忽略</summary>
		private string remoteTASP = "02 01";
        ///<summary >CPU所在的槽号，S7-300的PLC一般都为"02"，S7-400的PLC一般都为"03", S7-200/1200/1500的PLC一般都为"01"</summary>
        public virtual String getCPU_SlotNO()  //默认1500
       {
            return "01";
        }

        public override bool afterConnect() {
            String key = "";                   //03 00 00 16 11 E0 00 00 00 0a 00 C1 02 00 01 C2 02 02 02 C0 01 0a
            var t3 = FuckProtect.DataFrom(608);//03 00 00 16 11 E0 00 00 00 01 00 C0 01 09 C1 02 {0}   C2 02 {1}
            if( this.PLCtype == "1" ) {//”1”: S7-200/smart系列，执行
                key = string.Format(FuckProtect.DataFrom(608) ,this.remoteTASP ,this.localTASP);//数据合成
            } else {
                String t = "03 00 00 16 11 E0 00 00 00 01 00 C0 01 09 C1 02 4B 54 C2 02 03 {0}";
                key = string.Format(t ,getCPU_SlotNO());//”2”： S7-300/400/1200/1500系列
            }
            byte[] b = Siemens102Util.StrToArraybyte(key);
            Send(b);

            byte[] buffer = ReadTelex(1024);
            int num = buffer.Length;
            if( num == 22 ) {
                String t = "03 00 00 19 02 F0 80 32 01 00 00 C0 28 00 08 00 00 F0 00 00 01 00 01 00 F0";
                byte[] b2 = Siemens102Util.StrToArraybyte(t);
                Send(b2);
                buffer = ReadTelex(1024);
                num = buffer.Length;
                if( num == 27 ) {
                    initfinsh = 1;
                    return true;
                }
            } else {
                fileAll("请检查端口" + port + "与IP" + ip);
            }
            return false;
        }
    }
}
